/*
 * @Author: 王雁鹏
 * @Date: 2023-05-30 22:06:08
 * @LastEditTime: 2023-06-08 23:00:14
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \expressf:\web-vue\bs-nodejs-background-code\routes\backstage.js
 * -------------------------------------
 */
const request = require('request');
var req = require("../utils/router");
const db = require("../mysql/index.js");
let { router, GetRouter, PostRouter, Upload, DBMap } = req
let insert = ({ name, dbName, data, res, only }) => {
    let obj = DBMap(name, data)
    let sql = db.insert(dbName, { ...obj })
    let add = () => {
        db.query(sql, (rows, fields) => {
            res.send({
                code: 200,
                data: {}
            })
        })
    }
    if (only) {
        db.only(only, dbName, obj[only], (bool) => {
            if (!bool) {
                res.send({
                    code: 200,
                    data: {
                        msg: "code值为唯一值，不得重复"
                    }
                })
            } else {
                add()
            }
        })
    } else {
        add()
    }
}

// 设置类目
new PostRouter().send('/setcategory', function (res, data) {
    insert({
        name: 'category',
        dbName: 'wh_category',
        only: 'cg_code',
        data,
        res,
    })
})
// 注册按钮
new PostRouter().send('/register', function (res, data) {
    insert({
        name: 'user',
        dbName: 'user',
        data,
        res,
    })
})

new PostRouter().send('/addProduce', function (res, data) {
    insert({
        name: 'produce',
        dbName: 'wh_produce',
        data,
        res,
    })
})
// 产品设置
new PostRouter().send('/setProduce', function (res, data) {
    insert({
        name: 'produce',
        dbName: 'wh_produce',
        data,
        res,
    })
})


module.exports = router

